/**date:21-07-11
 * The following program implements binary search  on a given array,to find the 
 * position 
 */ 
//binsearch returns the p
def binSearch(xs:Array[Int],x:Int):Int={
    def binSearch1(low:Int,high:Int):Int ={
        if(xs.length==0) -1
        else if(xs.length==1) if(xs(0)!=x) -1 else 0
        else{
            var m=(Math.ceil((low+high).toDouble/2.0)).toInt
            if(x==xs(m)) m
            else if(x<xs(m))  {
                if(xs(m-1)<x) -1
                else binSearch1(0,m)
            }
            else binSearch1(m,xs.length-1)
        }        
    }
    binSearch1(0,xs.length-1)
}
test("binary search works") {
     binSearch(Array(),6) should equal(-1)
     binSearch(Array(57),56) should equal (-1)
     binSearch(Array(57),57) should equal (0)
     binSearch(Array(6,18,24,32),) should equal (-1)  
}